package com.wf.course.web.controller.member;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.utils.page.PageBean;
import com.wf.course.model.member.Member;
import com.wf.course.service.IMemberService;
import com.wf.course.web.Interceptor.LogInfo;
/**
 * 
 * @content 用户Controller
 */
@RequestMapping("member")
@Controller
public class MemberController {

	@Autowired
	private IMemberService memberService;

	/**
	 *  列表
	 * @param pageNo
	 * @param pageSize
	 * @param userName
	 * @param telephone
	 * @param userSource
	 * @param model
	 * @return
	 */
	@RequestMapping("memberList")
	public String chapterList(HttpSession httpSession,
			@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "userName", required = false) String userName, 
			@RequestParam(value = "telephone", required = false) String telephone, 
			@RequestParam(value = "userSource", required = false) String userSource, 
			@RequestParam(value = "blackFlag", required = false) String blackFlag, 
			Model model) {

		// 组装一个查询条件的map
		Map<String, Object> cons = new HashMap<String, Object>();
		cons.put("userName", userName);
		cons.put("telephone", telephone);
		cons.put("userSource", userSource);
		cons.put("blackFlag", blackFlag);
		
		PageBean<Member> pageBean = new PageBean<Member>(pageNo, pageSize, null, cons);
		pageBean.setUrl("member/memberList");
		this.memberService.indexInfo(pageBean);
		
		model.addAttribute("page", pageBean);
		httpSession.setAttribute("page", pageBean);
		
		return "member/memberList";
	}

	/**
	 *  修改和新增页面数据初始化
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("pageLoad/{id}")
	public String downLoadById(@PathVariable("id") Integer id, Model model,HttpSession httpSession) {
		 
		if (id != null && id != 0) {
			Member member = memberService.queryMemberById(id);
			model.addAttribute("member", member);
			model.addAttribute("type", "upd");
		}else{
			model.addAttribute("type", "add");
		}
		
		return "member/memberEdit";
	}

	/**
	 * 新增和修改
	 * @param member
	 * @param request
	 * @return
	 */
	@LogInfo("新增或修改用户信息")
	@RequestMapping(value = "memberEdit" , produces = "text/html;charset=UTF-8")
	public String memberEdit(@ModelAttribute Member member,HttpServletRequest request,HttpSession httpSession,Model model) {
		
		memberService.saveOrUpdateMember(member);
		
		//保存后，跳转到之前的列表页
		@SuppressWarnings("unchecked")
		PageBean<T> page =  (PageBean<T>) httpSession.getAttribute("page");
 		
		PageBean<Member> pageBean = new PageBean<Member>(page.getActulPageNo(), page.getPageSize(), null, page.getConMap());
		pageBean.setUrl("member/memberList");
		this.memberService.indexInfo(pageBean);
		
		model.addAttribute("page", pageBean);
 		
		return "member/memberList";
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@LogInfo("删除用户信息")
	@RequestMapping(value = "remove/{id}")
	public String removemember(@PathVariable("id") Integer id) {
		
		memberService.deleteById(id);
		
		return "redirect:/member/memberList";
	}
	
	
	@ResponseBody
	@RequestMapping(value="checkMember"  , method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	public String checkMember( 
			@RequestParam(value = "telephone", required = false) String telephone, 
			@RequestParam(value = "userId", required = false) Integer userId ){
		String msg = "";
		
		userId= userId==null ? 0 : userId;
		telephone= telephone==null ? "" : telephone;
				 
		List<Member> mbrs = memberService.queryMembersByTelAndId(telephone,userId);
		if(mbrs!=null && mbrs.size()>0){
			msg ="false";
		}else{
			msg ="true";
		}
		return msg;
	}

}
